package com.yixun.app.viewpagerandimag.sample;

import java.util.ArrayList;
import java.util.List;

import xf.tools.Other;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.ui.housing.HouSingResourcesDetailsActivity;
import com.yixun.app.viewpagerandimag.PhotoView;

/**
 * 
 * <p>
 * <b>Title：</b> 查看图片
 * </p>
 * <p>
 * <b>Description：</b> 进入该界面时，需要传入进入时的图片数组下标，使展示的与点击的图片相对应 大图查看<br>
 * 如果是头像，直接传入一个长度为1的list,且内容为错误链接，并type=99；
 * </p>
 * 
 * @author tangxin
 * @date 2016年12月6日 下午4:07:20
 */
public class ViewPagerActivity extends BaseActivity {

	private ViewPager mViewPager;
	/** 图片列表，若为本地，add前先拼接file:// */
	public static List<String> sDrawables = new ArrayList<String>();

	/** 首先展示的图片下标 */
	public static int postion = 0;

	/** 99为头像 */
	public static int type = 0;

	/** 不为0则可以删除 */
	private int IsOpenDelete = 0;

	/** 是否设定名字 0不设定 1设定为总平图 */
	private int IsSetName = 0;

	SamplePagerAdapter a;

	TextView count_viewpage;

	/** 1则是房源详情过来，删除需要调用接口 */
	private String HouSingResources = "0";

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mViewPager = new HackyViewPager(this);
		setContentView(mViewPager);
		BaseActivity.activity = this;

		IsOpenDelete = getIntent().getIntExtra("IsOpenDelete", 0);

		IsSetName = getIntent().getIntExtra("IsSetName", 0);

		HouSingResources = getIntent().getStringExtra("HouSingResources");

		View view = LayoutInflater.from(this).inflate(R.layout.viewpage_layout, null);
		addContentView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		ImageView back_btn = (ImageView) view.findViewById(R.id.back_btn);
		count_viewpage = (TextView) view.findViewById(R.id.count_viewpage);
		TextView textView1 = (TextView) view.findViewById(R.id.view_delete);

		SetRelativiLayoutPadding(R.id.Viewpager_rela);

		if (IsOpenDelete != 0) {
			textView1.setVisibility(View.VISIBLE);
		} else {
			textView1.setVisibility(View.INVISIBLE);
		}
		textView1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				popu(ViewPagerActivity.this, "是否删除该图片？", true);
			}
		});

		back_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		if (IsSetName == 0) {
			count_viewpage.setText("1/" + sDrawables.size());
		} else if (IsSetName == 1) {
			count_viewpage.setText("总平图" + "(" + ("1/" + sDrawables.size()) + ")");
		}
		a = new SamplePagerAdapter();
		mViewPager.setAdapter(a);
		mViewPager.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				System.out.println("s");
			}
		});
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				postion = arg0;
				if (IsSetName == 0) {
					count_viewpage.setText((arg0 + 1) + "/" + sDrawables.size());
				} else if (IsSetName == 1) {
					count_viewpage.setText("总平图" + "(" + ((arg0 + 1) + "/" + sDrawables.size()) + ")");
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		mViewPager.setCurrentItem(postion);
	}

	static class SamplePagerAdapter extends PagerAdapter {
		@Override
		public int getCount() {
			return sDrawables.size();
		}

		public void notifyd() {
			notifyDataSetChanged();
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(container.getContext());
			if (type == 99) {
				EmmApplication.image(R.drawable.touxiang);
			} else {
				EmmApplication.image(R.drawable.default_image);
			}

			EmmApplication.imageLoader.displayImage(sDrawables.get(position), photoView, EmmApplication.options);

			container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		BaseActivity.activity = this;
		IntentFilter filter = new IntentFilter(Other.FINISHVIEWPAGER);
		registerReceiver(mBroadcastReceiver, filter);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mBroadcastReceiver);
	}

	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Other.FINISHVIEWPAGER)) {
				finish();
			}
		}
	};

	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
	}

	public View view3;
	public PopupWindow popu_call;

	/** 上下文对象 提示内容 是否展示取消按钮 type = 0 正常模式，确定按钮实际功能 ,为99是退出登录功能 */
	public void popu(final Activity activity, String msg, boolean isVscancel) {
		view3 = LayoutInflater.from(activity).inflate(R.layout.popu_shuerdp, null);
		popu_call = new PopupWindow(view3, android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT,
				android.support.v4.view.ViewPager.LayoutParams.FILL_PARENT);
		popu_call.setFocusable(true);

		// EmmApplication.SetNumberLog(activity, 0.5f);

		popu_call.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
		popu_call.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

		popu_call.showAtLocation(view3, Gravity.CENTER, 0, 0);
		popu_call.showAsDropDown(view3, 0, 0);
		popu_call.setFocusable(true);
		popu_call.setOutsideTouchable(true);
		view3.setFocusable(true);
		view3.setFocusableInTouchMode(true);

		view3.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
				}
				return false;
			}
		});
		// 取消
		TextView dismiss = (TextView) view3.findViewById(R.id.dismiss);
		// 确定
		TextView shuerbtn = (TextView) view3.findViewById(R.id.shuerbtn);

		TextView ts_message = (TextView) view3.findViewById(R.id.ts_message);
		ts_message.setText(msg);
		if (isVscancel) {
			dismiss.setVisibility(View.VISIBLE);
		} else {
			dismiss.setVisibility(View.GONE);
		}
		dismiss.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				popu_call.dismiss();
				// WindowManager.LayoutParams lp =
				// activity.getWindow().getAttributes();
				// lp.alpha = 1f;
				// activity.getWindow().setAttributes(lp);
			}
		});
		shuerbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				popu_call.dismiss();
				// EmmApplication.SetLog(activity);

				EmmApplication.IsCreate = true;

				if ("1".equals(HouSingResources)) {
					// 调用删除
					Log.v("需要删除", "路径：" + sDrawables.get(postion));
					HouSingResourcesDetailsActivity.DeleteUrl = sDrawables.get(postion);
					sendBroadcast(new Intent(EmmApplication.NEWHousing));

					// 触发删除
				}

				String item_sdeawables = "";
				if (sDrawables.size() >= 1 && postion < sDrawables.size()) {
					item_sdeawables = sDrawables.get(postion);
					sDrawables.remove(postion);
				}
				if (EmmApplication.pir.size() >= 1 && postion < EmmApplication.pir.size()) {
					for (int i = 0; i < EmmApplication.pir.size(); i++) {
						if (("file://" + EmmApplication.pir.get(i).sourcePath).equals(item_sdeawables)
								|| EmmApplication.pir.get(i).sourcePath.equals(item_sdeawables)) {// 本地路径或服务器图片
							EmmApplication.pir.remove(postion);
							continue;
						}
					}
				}

				if (sDrawables.size() < 1) {
					finish();
				} else {
					if (postion >= sDrawables.size()) {
						postion--;
					}
				}
				a = new SamplePagerAdapter();
				mViewPager.setAdapter(a);
				mViewPager.setCurrentItem(postion);
				count_viewpage.setText((postion + 1) + "/" + sDrawables.size());

			}
		});
	}
}